package com.jhws.business.device.service;

import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.business.device.bo.LogDevUnlock;
import com.jhws.core.db.BaseDao;
import com.jhws.util.DateUtil;
import com.jhws.util.GetId;
import com.jhws.util.LogHelper;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class LogDevUnlockService {
    @Resource
    private BaseDao BaseDao;
    @Resource
    private OSSClientService ossClientService;

    /**
     * 保存开锁记录
     *
     * @param logDevUnlock 开锁记录
     */
    public void saveLogDevUnlockInfo(LogDevUnlock logDevUnlock) {
        if (StringUtils.isEmpty(logDevUnlock.getId())) {
            logDevUnlock.setId(GetId.randomID());
        }
        BaseDao.saveOrUpdate(logDevUnlock);
    }

    /**
     * 分页查询开锁记录
     */
    public Map findFeedbackListPage(String commCode, String timeStart, String timeEnd, String type, String building, String unit, String ownerName, String ownerMob) {
        String whereClause = "where 1=1 ";
        if (StringUtils.isNotEmpty(commCode)) {
            whereClause += " and comm_code = '" + commCode + "'";
        }
        if (StringUtils.isNotEmpty(type)) {
            whereClause += " and type = '" + type + "'";
        }
        if (StringUtils.isNotEmpty(ownerMob)) {
            whereClause += " and owner_mob = '" + ownerMob + "'";
        }
        if (StringUtils.isNotEmpty(ownerName)) {
            whereClause += " and owner_name = '" + ownerName + "'";
        }
        if (StringUtils.isNotEmpty(building)) {
            whereClause += " and building = '" + building + "'";
        }
        if (StringUtils.isNotEmpty(unit)) {
            whereClause += " and unit = '" + unit + "'";
        }
        if (StringUtils.isNotEmpty(timeStart)) {
            whereClause += " and time >= '" + timeStart + " 00:00:00'";
        }
        if (StringUtils.isNotEmpty(timeEnd)) {
            whereClause += " and time <= '" + timeEnd + " 00:00:00'";
        }

        Map map = BaseDao.queryPage("from LogDevUnlock " + whereClause + " order by time desc");
        List<LogDevUnlock> logDevUnlocks = (List<LogDevUnlock>) map.get("list");
        if (logDevUnlocks != null && logDevUnlocks.size() > 0) {
            for (LogDevUnlock logDevUnlock : logDevUnlocks) {
                logDevUnlock.setImageUrl(ossClientService.getUrl(logDevUnlock.getImageUrl(), false));
            }
        }
        return map;
    }

    //删除3个月之前的数据
    public void delete3Month() {
        String hql = "delete LogDevUnlock  where time <= '" + DateUtil.getDateByFrontMonth(3, DateUtil.TIME_FORMAT)+"'";
        LogHelper.e("------------------>    " + hql);
        BaseDao.executeHql(hql);
    }
}
